package com.zy.cat.timing;

import com.alipay.api.domain.DataEntry;
import com.zy.cat.entity.CatUserWelfareCentr;
import com.zy.cat.service.CatUserWelfareCentrService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
@Slf4j
public class WelfareCentrTiming {

    @Autowired
    private CatUserWelfareCentrService catUserWelfareCentrService;


    /**
     *
     * 每天0点更新福利中心
     */
    @Scheduled(cron = "0 0 0 */1 * ?")
    public void updateWelfareCentr(){
        log.info("每天0点更新用户福利中心数据"+new Date());
        //查询所有用户的福利中心
        List<CatUserWelfareCentr> allWelfareList = catUserWelfareCentrService.getAllCatUserWelfareCentr();
        allWelfareList.forEach(v->{
            if(v.getTodaySigninStatus()  == 0 && v.getSigninSum()!=0
                    || v.getSigninSum() == 7 ){
                v.setSigninSum(0);
            }
            v.setTodaySigninStatus(0);
            v.setTodayGold(0);
            v.setIntoBookrack(0);
            v.setTodayRead(0);
            v.setInviter(0);
            v.setVideo(0);
            v.setReadTen(0);
            v.setReadThirty(0);
            v.setReadSixty(0);
            v.setReadOneTwoZero(0);
            v.setReadOneEightZero(0);
            //更新用户福利中心数据
            Map<String,Object> map = new HashMap<>();
            map.put("pid",v.getPid());
            map.put("gold",v.getGold());
            map.put("signinRemind",v.getSigninRemind());
            map.put("todaySigninStatus",v.getTodaySigninStatus());
            map.put("signinSum",v.getSigninSum());
            map.put("todayGold",v.getTodayGold());
            map.put("intoBookrack",v.getIntoBookrack());
            map.put("todayRead",v.getTodayRead());
            map.put("inviter",v.getInviter());
            map.put("video",v.getVideo());
            map.put("readTen",v.getReadTen());
            map.put("readThirty",v.getReadThirty());
            map.put("readSixty",v.getReadSixty());
            map.put("readOneTwoZero",v.getReadOneTwoZero());
            map.put("readOneEightZero",v.getReadOneEightZero());
            int i = catUserWelfareCentrService.updateCatUserWelfareCentrByPid(map);
           while(i==0){
               i= catUserWelfareCentrService.updateCatUserWelfareCentrByPid(map);
            }
        });
    }
}
